package jianbo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.google.protobuf.ByteString;
import utgame.UtMessage.MS_PlayerOnline;
import utgame.UtMessage.MS_PlayerOffline;
import utgame.UtMessage.MessagePacket.MessageType;
import utgame.UtMessage.MS_OfflineChat;

/**
 * Created by xueal on 2018/1/17.
 */
// 仅管理在线玩家
public class ClientSession {

    private static final ClientSession single = new ClientSession();

    public static ClientSession getInstance() {return single;}

    private HashMap<String, WebSocketController> mClientMap = new HashMap<>();

    public void setClient(String name, WebSocketController client){
        mClientMap.put(name, client);
    }
    public WebSocketController getClient(String name){
        return mClientMap.get(name);
    }
    public String getClientName(WebSocketController client){
        for (Map.Entry<String, WebSocketController> entry : mClientMap.entrySet()) {
            if (client == entry.getValue()){
                return entry.getKey();
            }
        }
        return null;
    }

    public void removeClient(String name){
        mClientMap.remove(name);
    }
    public void removeClient(WebSocketController client){
        for (Map.Entry<String, WebSocketController> entry : mClientMap.entrySet()) {
            if (client == entry.getValue()){
                mClientMap.remove(entry.getKey());
                break;
            }
        }
    }

    public void broadcastPlayerOnline(String name){

        MS_PlayerOnline.Builder builder = MS_PlayerOnline.newBuilder();
        builder.setName(name);

        for (Map.Entry<String, WebSocketController> entry : mClientMap.entrySet()) {
            try {
                entry.getValue().sendMessage(MessageType.PlayerOnline, builder.build().toByteString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void broadcastPlayerOffline(String name){

        MS_PlayerOffline.Builder builder = MS_PlayerOffline.newBuilder();
        builder.setName(name);

        for (Map.Entry<String, WebSocketController> entry : mClientMap.entrySet()) {
            try {
                entry.getValue().sendMessage(MessageType.PlayerOffline, builder.build().toByteString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void sendOfflineChat(String name){

        WebSocketController client = getClient(name);
        if (client == null) return;

        ArrayList<byte[]> offlineChatList = PlayerManager.getInstance().getOfflineChat(name);

        MS_OfflineChat.Builder builder = MS_OfflineChat.newBuilder();

        for (byte[] bytes : offlineChatList){
            builder.addOfflineChat(ByteString.copyFrom(bytes));
        }

        try {
            client.sendMessage(MessageType.OfflineChat, builder.build().toByteString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void broadcastPlayerAction(MessageType msgType, com.google.protobuf.ByteString message){

        for (Map.Entry<String, WebSocketController> entry : mClientMap.entrySet()) {
            try {
                entry.getValue().sendMessage(msgType, message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
